import Vue from 'vue'
import Vuex from 'vuex'
// 引入vuex
Vue.use(Vuex);
// 初始化store
const store = new Vuex.Store({
  // 存储的数据
  state: JSON.parse(localStorage.getItem("user")) || {
    mesNumbers: {
      warehouse: 0,
      temperature: 0,
      archives: 0,
      fire: 0
      }
     
  },
  // 更改状态的函数
  mutations: {
    /**
     * 更改异常信息的数量
     * messageArray=[{
     * Type:"warehouse",
     * Count:4
     * }]
     * @param {Array} messageArray 
     */
    changeMesNumber(state, messageArray) {
      var mesNumbers = state.mesNumbers;
      messageArray.forEach(item => {
        switch (item.Type) {
          case "warehouse":
            {
            mesNumbers.warehouse += Number(item.Count);
              break;
            }
          case "temperature":
            {
              mesNumbers.temperature += Number(item.Count);
              break;
            }
          case "archives":
            {
              mesNumbers.archives += Number(item.Count);
              break;
            }
          case "fire":
            {
              mesNumbers.fire += Number(item.Count);
              break;
            }

        }
      });
      state.mesNumbers = mesNumbers;
      // 同步更新本地数据信息，防止页面刷新丢失
      localStorage.setItem("user", JSON.stringify(state));
      console.log("store:", state);
    },
/**
 * 页加载修改当前异常信息的数量为0
 * 
 * messageType :异常信息的类型
 * @param {string} messageType 
 */
clearMesNumber(state, messageType) {
      switch (messageType) {
        case "warehouse":
          {
            state.mesNumbers.warehouse = 0;
            break;
          }
        case "temperature":
          {
            state.mesNumbers.temperature = 0;
            break;
          }
        case "archives":
          {
            state.mesNumbers.archives = 0;
            break;
          }
        case "fire":
          {
            state.mesNumbers.fire = 0;
            break;
          }
      }
       // 同步更新本地数据信息，防止页面刷新丢失
      localStorage.setItem("user", JSON.stringify(state));
    }

  }
});
export default store;
